<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class ResultsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	
	public function saveResults($results)
    {
		$data = array(
			'user_id' 	  			=> $results['user_id'],
			'course_id'				=> $results['course_id'],
			'question_id' 	  		=> $results['question_id'], 	
			'answer_id' 	  		=> $results['answer_id'], 
			'user_answer'			=> $results['user_answer'],
			'status' 				=> 1, 				
		);
		
		$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }
	public function getResults($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_answer', new Expression('pedia_answer.answer_id=pedia_results.answer_id'),array('correct_answer'),'left');
	    $select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_results.course_id'),array('course_name'),'left');
		$select->join('pedia_questions', new Expression('pedia_questions.question_id=pedia_results.question_id'),array('question_name','level'),'left');
		$select->where('pedia_answer.correct_answer=pedia_results.user_answer');
		$select->where('pedia_results.user_id="'.$user_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	}
}